package com.yuzhitong.shapi.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.flyco.tablayout.SlidingTabLayout;
import com.yuzhitong.shapi.R;
import com.yuzhitong.shapi.activity.CategoryActivity;
import com.yuzhitong.shapi.activity.MovieSearchActivity;
import com.yuzhitong.shapi.activity.PlayHistoryActivity;
import com.yuzhitong.shapi.base.BaseMvpFragment;
import com.yuzhitong.shapi.base.Contents;
import com.yuzhitong.shapi.bean.CategoryBean;
import com.yuzhitong.shapi.contract.MovieContract;
import com.yuzhitong.shapi.presenter.MoviePresenter;
import com.yuzhitong.shapi.util.UiUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Brsya
 * CreateDate: 2022/5/8  14:30
 * Description:
 * 影视页
 */
public class MovieFragment extends BaseMvpFragment<MoviePresenter> implements MovieContract.View, MovieListFragment.OnSearchTextChange, View.OnClickListener {

    private final String[] adIds = new String[]{Contents.AD_TT_SDK_MOVIE_ID
            , Contents.AD_TT_SDK_MOVIE_ID_1
            , Contents.AD_TT_SDK_MOVIE_ID_2
            , Contents.AD_TT_SDK_MOVIE_ID_3
            , Contents.AD_TT_SDK_MOVIE_ID_4
            , Contents.AD_TT_SDK_MOVIE_ID_5
            , Contents.AD_TT_SDK_MOVIE_ID_6
    };


    private LinearLayout llMovieSearch;
    private TextView tvSearchHot;
    private LinearLayout llCategoryBox;
    private LinearLayout llCategoryBar;
    private SlidingTabLayout tlMovieTitle;
    private ViewPager vpMovieBox;

    private List<String> tabs;
    private List<Fragment> fragmentList;

    private String hotKey;
    private String hotKeyName;

    private FragmentPagerAdapter adapter;
    private LinearLayout llHistoryBox;

    public static MovieFragment newInstance() {

        Bundle args = new Bundle();

        MovieFragment fragment = new MovieFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected void initView() {
        setContentViewId(R.layout.fragment_movie);
        llMovieSearch = findViewById(R.id.ll_movie_search);
        tvSearchHot = findViewById(R.id.tv_search_hot);
        llCategoryBox = findViewById(R.id.ll_category_box);
        tlMovieTitle = findViewById(R.id.tl_movie_title);
        vpMovieBox = findViewById(R.id.vp_movie_box);
        llCategoryBar = findViewById(R.id.ll_category_bar);
        llHistoryBox = findViewById(R.id.ll_history_box);
    }

    @Override
    protected void initData() {
        tabs = new ArrayList<>();
        fragmentList = new ArrayList<>();

        UiUtil.setStatusPadding(getContext(), findViewById(R.id.cl_movie_page_box));

        presenter.getCategory();
        presenter.getPlayAddressNew();

        llMovieSearch.setOnClickListener(this);
        llCategoryBox.setOnClickListener(this);
        llHistoryBox.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {

        int id = v.getId();
        if (id == R.id.ll_movie_search) {
            // 2022/5/8 搜索页
            Intent intent = new Intent(getActivity(), MovieSearchActivity.class);
            startActivity(intent);
        } else if (id == R.id.ll_history_box) {
            Intent intent = new Intent(getActivity(), PlayHistoryActivity.class);
            startActivity(intent);
        } else if (id == R.id.ll_category_box) {
            Intent intent = new Intent(getActivity(), CategoryActivity.class);
            startActivity(intent);
        }

    }

    private void initFragment() {
        vpMovieBox.setOffscreenPageLimit(10);
        vpMovieBox.setAdapter(adapter = new FragmentPagerAdapter(getChildFragmentManager(), FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {
            @NonNull
            @Override
            public Fragment getItem(int position) {
                return fragmentList.get(position);
            }

            @Override
            public int getCount() {
                return fragmentList.size();
            }

            @Nullable
            @Override
            public CharSequence getPageTitle(int position) {
                return tabs.get(position);
            }
        });

        vpMovieBox.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                for (int i = 0; i < tabs.size(); i++) {
                    if (position == i) {
                        tlMovieTitle.getTitleView(i).setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
                    } else {
                        tlMovieTitle.getTitleView(i).setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
                    }
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        tlMovieTitle.setViewPager(vpMovieBox);
        tlMovieTitle.getTitleView(0).setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
    }

    @Override
    protected void initPresenter() {
        presenter = new MoviePresenter();
        presenter.attachView(this);
    }

    @Override
    public void onResume() {
        super.onResume();
        if (getActivity() != null) {
            getActivity().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        }
    }

    @Override
    public void showCategory(List<CategoryBean> categoryBeans) {
        Map<String, String> tabTitle = new HashMap<>();
        for (CategoryBean categoryBean : categoryBeans) {
            if ("type".equals(categoryBean.getCode())) {
                for (CategoryBean.ChailDTO chailDTO : categoryBean.getChail()) {
                    tabs.add(chailDTO.getCategoryName());
                    tabTitle.put(chailDTO.getCategoryName(), chailDTO.getCode());
                }
                continue;
            }
            if ("sort".equals(categoryBean.getCode())) {
                if (categoryBean.getChail() != null && categoryBean.getChail().size() > 0) {
                    hotKey = categoryBean.getChail().get(0).getCode();
                    hotKeyName = categoryBean.getChail().get(0).getCategoryName();
                }
            }
        }
        int position = 0;

        for (int i = 0; i < tabs.size(); i++) {

            position = i;
            while (position > adIds.length) {
                position -= adIds.length;
            }
            String ad = adIds[position];

            String tab = tabs.get(i);
            MovieListFragment movieListFragment = MovieListFragment.newInstance(tabTitle.get(tab), hotKey, hotKeyName, i == 0, ad);
            if (i == 0) {
                movieListFragment.setOnSearchTextChange(this);
            }
            //            MovieListOnlyFragment movieListFragment = MovieListOnlyFragment.newInstance(tabTitle.get(tab), hotKey, i == 0);
            //            if (i == 0) {
            //                movieListFragment.setOnSearchTextChange(this);
            //            }
            fragmentList.add(movieListFragment);
        }

        vpMovieBox.setOffscreenPageLimit(fragmentList.size());
        initFragment();
    }

    @Override
    public void changeSearchText(String text) {
    }
}
